package Thread;

/**
 * @author liyang
 * @version 1.0
 * @package Thread
 * @file DeadLockDemo.java
 * @description
 * @time 2024-04-14 12:19
 */
public class DeadLockDemo {
    /*使其成为死锁的要点：让两边线程先后拿到一个信号量（通过睡眠），然后让他俩开始争抢对方手中的临界资源*/
    private static Object kz = new Object();
    private static Object sz = new Object();
    public static void main(String[] args) {
        Thread np = new Thread(){
            @Override
            public void run(){
                System.out.println("1-1");
                synchronized (kz){
                    System.out.println("1-2");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (sz){
                        System.out.println("1-3");
                    }
                    System.out.println("1-4");
                }
                System.out.println("1-5");
            }


        };

        Thread sp = new Thread(){
            @Override
            public void run(){
                System.out.println("2-1");
                synchronized (sz){
                    System.out.println("2-2");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (kz){
                        System.out.println("2-3");
                    }
                    System.out.println("2-4");
                }
                System.out.println("2-5");
            }


        };

        np.start();
        sp.start();

    }
}
